package com.athuahua.spzx.service.impl;

import cn.hutool.core.date.DateTime;
import com.athuahua.spzx.entity.ProductDetails;
import com.athuahua.spzx.entity.ProductSku;
import com.athuahua.spzx.entity.vo.ProductVo;
import com.athuahua.spzx.exception.CodeException;
import com.athuahua.spzx.mapper.ProductDetailsMapper;
import com.athuahua.spzx.mapper.ProductSkuMapper;
import com.athuahua.spzx.dto.product.ProductDto;
import com.athuahua.spzx.entity.ProductSaveUpdateEntity;
import com.athuahua.spzx.utils.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.Product;
import com.athuahua.spzx.service.ProductService;
import com.athuahua.spzx.mapper.ProductMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author huahua
* @description 针对表【product(商品)】的数据库操作Service实现
* @createDate 2024-08-01 15:06:56
*/
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductSkuMapper productSkuMapper;

    @Resource
    private ProductDetailsMapper productDetailsMapper;


    @Override
    public IPage<ProductVo> pageInfo(Integer page, Integer limit, ProductDto productDto) {
        IPage<ProductVo> iPage = new Page<>(
                page,
                limit
        );
        IPage<ProductVo> productVos = productMapper.IpageInfo(iPage, productDto);
        return productVos;
    }

    /**
     * 添加要操作的三张表
     * product表 商品基本信息
     * product_sku表 商品的sku信息表
     * product_detail表 商品的详情信息表
     */
    @Override
    @Transactional
    public void iSave(ProductSaveUpdateEntity productSaveUpdateEntity) {
        //添加状态时间等信息
        productSaveUpdateEntity.setCreateTime(DateTime.now());
        productSaveUpdateEntity.setUpdateTime(DateTime.now());
        productSaveUpdateEntity.setStatus(0);
        productSaveUpdateEntity.setAuditStatus(0);
        //1.保存商品基本信息到product表中
        Product product = new Product();
        BeanUtils.copyProperties(productSaveUpdateEntity, product);
        productMapper.insert(product);

        //2.获取sku列表集合，保存sku信息
        insertProductSku(productSaveUpdateEntity, product);

        //3.保存详情数据，
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(productSaveUpdateEntity.getDetailsImageUrls());
        productDetails.setCreateTime(DateTime.now());
        productDetails.setUpdateTime(DateTime.now());
        productDetailsMapper.insert(productDetails);

    }

    @Override
    public ProductSaveUpdateEntity iGetById(Integer productId) {


        //1.根据id查询商品基本信息 product
        Product product = productMapper.selectById(productId);
        if(product == null){
            throw new CodeException(ResultCodeEnum.DATA_ERROR);
        }

        //2.根据id查询商品sku信息列表
        List<ProductSku> productSkus = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, productId)
        );

        //3.更具id查询商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectOne(
                new LambdaQueryWrapper<ProductDetails>()
                        .eq(ProductDetails::getProductId, productId)
        );

        //构建
        ProductSaveUpdateEntity result = new ProductSaveUpdateEntity();
        BeanUtils.copyProperties(product, result);
        result.setProductSkuList(productSkus);
        if(productDetails != null){
            result.setDetailsImageUrls(productDetails.getImageUrls());
        }


        return result;
    }

    @Override
    @Transactional
    public void iUpdate(ProductSaveUpdateEntity productUpdate) {
        productUpdate.setUpdateTime(DateTime.now());
        productUpdate.setAuditStatus(0);
        productUpdate.setAuditMessage("");
        productUpdate.setStatus(0);
        //更新商品基本信息
        Product product = new Product();
        BeanUtils.copyProperties(productUpdate, product);
        productMapper.updateById(product);
        //更新商品sku信息 先删除后插入
        productSkuMapper.delete(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, productUpdate.getId())
        );
        insertProductSku(productUpdate, product);
        //更新商品详情信息
        ProductDetails productDetails = productDetailsMapper.selectOne(
                new LambdaQueryWrapper<ProductDetails>()
                        .eq(ProductDetails::getProductId, productUpdate.getId())
        );
        if(productDetails == null){
            productDetails = new ProductDetails();
            productDetails.setProductId(productUpdate.getId());
            productDetailsMapper.insert(productDetails);
        }
        productDetails.setImageUrls(productUpdate.getDetailsImageUrls());



        productDetailsMapper.updateById(productDetails);


    }

    @Override
    @Transactional
    public void iRemoveById(Integer id) {
        productMapper.deleteById(id);
        productSkuMapper.delete(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, id)
        );
        productDetailsMapper.delete(new LambdaQueryWrapper<ProductDetails>()
                .eq(ProductDetails::getProductId, id)
        );

    }

    @Override
    public void iUpdateAuditStatus(Integer id, Integer status) {
        Product product = productMapper.selectById(id);
        if(product == null){
            throw new CodeException(ResultCodeEnum.DATA_ERROR);
        }
        if(status == 1){
            product.setAuditStatus(1);
            product.setAuditMessage("审核通过");
        }else{
            product.setAuditStatus(-1);
            product.setAuditMessage("审核不通过");
        }
        product.setUpdateTime(DateTime.now());
        productMapper.updateById(product);
    }

    @Override
    public void iUpdateStatus(Integer id, Integer status) {
        Product product = productMapper.selectById(id);
        if(product == null){
            throw new CodeException(ResultCodeEnum.DATA_ERROR);
        }
        product.setStatus(status);
        product.setUpdateTime(DateTime.now());
        productMapper.updateById(product);
    }

    private void insertProductSku(ProductSaveUpdateEntity productUpdate, Product product) {
        List<ProductSku> productSkuList = productUpdate.getProductSkuList();
        for(int i = 0; i < productSkuList.size(); i++){
            ProductSku item = productSkuList.get(i);
            //修改时，skuId不为空，新增时候为空
            item.setId(null);
            item.setSkuCode(product.getId() + "_" + i);
            item.setProductId(product.getId());
            item.setCreateTime(DateTime.now());
            item.setUpdateTime(DateTime.now());
            item.setThumbImg(product.getSliderUrls());
            //skuName
            item.setSkuName(product.getName() + item.getSkuSpec());
            //销量 默认状态
            item.setSaleNum(0);
            item.setStatus(0);
            productSkuMapper.insert(item);
        }
    }
}




